package com.ylbh.modules.banner.controller;

import com.ylbh.common.annotation.SysLog;
import com.ylbh.common.pojo.banner.dto.BannerListDTO;
import com.ylbh.common.utils.CommonResult;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.common.utils.Ref;
import com.ylbh.modules.banner.entity.AppHomeBanner;
import com.ylbh.modules.banner.service.AppHomeBannerService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 楼层管理
 * @author mgh
 */
@RestController
@RequestMapping("home/floor")
public class HomeFloorController {

    @Autowired
    private AppHomeBannerService appHomeBannerService;

    @ApiOperation("floor列表")
    @RequestMapping("queryFloorList")
    @RequiresPermissions("home:floor:queryFloorList")
    public CommonResult<PageUtils> queryBannerList(BannerListDTO bannerListDTO) {
        if(StringUtils.isBlank(bannerListDTO.getBannerType())){
            bannerListDTO.setBannerType("4");
        }
        PageUtils list = appHomeBannerService.queryBannerLists(bannerListDTO);
        return Ref.ok(list);
    }

    @ApiOperation("通过id获取楼层信息")
    @RequestMapping("getFloorById/{id}")
    @RequiresPermissions("home:floor:getFloorById")
    public CommonResult<AppHomeBanner> getFloorById(@PathVariable Integer id) {
        AppHomeBanner floor = appHomeBannerService.getFloorById(id);
        return Ref.ok(floor);
    }

    @SysLog("修改floor")
    @ApiOperation("修改floor")
    @RequestMapping("modifyFloorById")
    @RequiresPermissions("home:floor:modify")
    public CommonResult modifyFloorById(@RequestBody AppHomeBanner appHomeBanner) {
        appHomeBannerService.updateById(appHomeBanner);
        return Ref.ok();
    }

    @SysLog("新增floor")
    @ApiOperation("新增floor")
    @RequestMapping("add")
    @RequiresPermissions("home:floor:add")
    public CommonResult add(@RequestBody AppHomeBanner appHomeBanner) {
        appHomeBanner.setAddTime(new Date());
        appHomeBanner.setBannerType("4");
        AppHomeBanner banner1 = new AppHomeBanner();
        BeanUtils.copyProperties(appHomeBanner, banner1);
        if (appHomeBanner.getAppType() == 0) {
            for (int i = 0; i < 3; i++) {
                banner1.setAppType(i + 1);
                appHomeBannerService.save(banner1);
            }
        } else {
            banner1.setAppType(null);
            appHomeBannerService.save(banner1);
        }
        return Ref.ok();
    }

    @SysLog("删除floor")
    @ApiOperation("删除floor")
    @RequestMapping("remove")
    @RequiresPermissions("home:floor:remove")
    public CommonResult remove(@RequestBody Integer[] ids) {
        AppHomeBanner appHomeBanner = new AppHomeBanner();
        appHomeBanner.setDeleteStatus(true);
        for (Integer id :ids) {
            appHomeBanner.setId(id.longValue());
            appHomeBannerService.updateById(appHomeBanner);
        }
        return Ref.ok();
    }

    @ApiOperation("vip升级商品列表")
    @GetMapping("/vipUpgradeGoodsList")
    public CommonResult vipUpgradeGoodsList(BannerListDTO dto) {
        PageUtils page = appHomeBannerService.vipUpgradeGoodsList(dto);
        return Ref.ok(page);
    }

    @SysLog("新增vip页面")
    @ApiOperation("新增vip页面")
    @PostMapping("addVipGoods")
    public CommonResult addVipGoods(@RequestBody AppHomeBanner appHomeBanner) {
        appHomeBanner.setAddTime(new Date());
        appHomeBannerService.save(appHomeBanner);
        return Ref.ok();
    }

}
